package edu.ynu.se.xiecheng.achitectureclass.common.controller;
import edu.ynu.se.xiecheng.achitectureclass.common.service.SensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sensor")
public class SensorController {

    @Autowired
    private SensorService sensorService;

    // 封装成功响应数据的类
    public static class SuccessResponse {
        private String message;
        private String data;

        // 构造函数、getter 和 setter
        public SuccessResponse(String message, String data) {
            this.message = message;
            this.data = data;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }
    }

    // 封装错误响应数据的类
    public static class ErrorResponse {
        private String error;
        private String message;

        // 构造函数、getter 和 setter
        public ErrorResponse(String error, String message) {
            this.error = error;
            this.message = message;
        }

        public String getError() {
            return error;
        }

        public void setError(String error) {
            this.error = error;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    @PostMapping("/fetchData")
    public ResponseEntity<?> fetchSensorData(@RequestParam String boxNo) {
        try {
            // 调用 service 层的方法，获取数据
            String fetchedData = sensorService.fetchAndSaveSensorData(boxNo);

            // 打印返回的结果
            System.out.println("Fetched Data: " + fetchedData);  // 打印数据

            // 返回成功响应，并将返回的数据封装成 JSON
            SuccessResponse successResponse = new SuccessResponse(
                    "Data fetched and saved successfully", fetchedData
            );
            return ResponseEntity.ok(successResponse);
        } catch (Exception e) {
            // 打印异常信息
            e.printStackTrace();  // 打印栈跟踪

            // 返回错误响应，包含错误信息的 JSON 格式
            ErrorResponse errorResponse = new ErrorResponse(
                    "Error", "An error occurred while fetching or saving data."
            );
            return ResponseEntity.status(500).body(errorResponse);
        }
    }
    @GetMapping("/data")
    public ResponseEntity<?> getSensorData(@RequestParam String sensor) {
        try {
            // 调用 service 层获取传感器数据
            Map<String, List<?>> sensorData = sensorService.getSensorData(sensor);

            // 返回数据给前端
            return ResponseEntity.ok(sensorData);
        } catch (IllegalArgumentException e) {
            // 如果传感器类型不正确，返回 400 错误
            return ResponseEntity.status(400).body("Invalid sensor type: " + sensor);
        } catch (Exception e) {
            // 处理其他异常
            e.printStackTrace();
            return ResponseEntity.status(500).body("Error fetching sensor data");
        }
    }

}
